<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="报抽人" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="报抽人"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="contact">
      <el-input v-model="dataForm.contact" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="抽取方式" prop="pumpMethod">
      <el-input v-model="dataForm.pumpMethod" placeholder="抽取方式"></el-input>
    </el-form-item>
    <el-form-item label="抽取时间" prop="pumpTime">
      <el-input v-model="dataForm.pumpTime" placeholder="抽取时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="descp">
      <el-input v-model="dataForm.descp" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="关联村域id" prop="countryId">
      <el-input v-model="dataForm.countryId" placeholder="关联村域id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          creator: '',
          contact: '',
          pumpMethod: '',
          pumpTime: '',
          status: '',
          descp: '',
          createTime: '',
          updateTime: '',
          countryId: ''
        },
        dataRule: {
          creator: [
            { required: true, message: '报抽人不能为空', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          pumpMethod: [
            { required: true, message: '抽取方式不能为空', trigger: 'blur' }
          ],
          pumpTime: [
            { required: true, message: '抽取时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          descp: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          countryId: [
            { required: true, message: '关联村域id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/pumptask/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.creator = data.pumpTask.creator
                this.dataForm.contact = data.pumpTask.contact
                this.dataForm.pumpMethod = data.pumpTask.pumpMethod
                this.dataForm.pumpTime = data.pumpTask.pumpTime
                this.dataForm.status = data.pumpTask.status
                this.dataForm.descp = data.pumpTask.descp
                this.dataForm.createTime = data.pumpTask.createTime
                this.dataForm.updateTime = data.pumpTask.updateTime
                this.dataForm.countryId = data.pumpTask.countryId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/pumptask/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'creator': this.dataForm.creator,
                'contact': this.dataForm.contact,
                'pumpMethod': this.dataForm.pumpMethod,
                'pumpTime': this.dataForm.pumpTime,
                'status': this.dataForm.status,
                'descp': this.dataForm.descp,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'countryId': this.dataForm.countryId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
